import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/models/data_image_model.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class DataImage extends StatefulWidget {
  final List<Map> dataList;

  final String title;

  DataImage({Key? key,required this.dataList,required this.title}) : super(key: key);
  @override
  _DataImageState createState() => _DataImageState();
}

class _DataImageState extends State<DataImage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return SfCartesianChart(
      plotAreaBorderWidth: 0,
      legend: Legend(isVisible: true,position: LegendPosition.right,textStyle: TextStyle(color: Colors.white)),
      title: ChartTitle(text:widget.title,textStyle: TextStyle(color: Colors.white,fontSize: 25.sp)),
      primaryXAxis: CategoryAxis(
          majorGridLines: MajorGridLines(width: 0),
          labelPlacement: LabelPlacement.onTicks),
      palette: <Color>[
        Colors.orange,
        Colors.yellowAccent,
        Colors.deepOrangeAccent,
        Colors.teal,
        Colors.blueAccent
      ],
      series: _getSeries(),
      tooltipBehavior: TooltipBehavior(enable: true),
    );
  }

  List<SplineSeries<ChartSampleData, String>> _getSeries() {
    var tableName = [{'tabName':'出库','data':[{'x':'1','y':1}]}];
    List<List<ChartSampleData>> chartData = [];
    for (var element in widget.dataList){
      List<ChartSampleData> list = [];
      for(var element in element['data']){
        list.add(ChartSampleData(element['x'],element['y']));
      }
      chartData.add(list);
    }
    final List fixedList = Iterable<int>.generate(chartData.length).toList();
    return fixedList.map((i) => SplineSeries<ChartSampleData, String>(
        dataSource: chartData[i],
        xValueMapper: (ChartSampleData sales, _) => sales.x,
        yValueMapper: (ChartSampleData sales, _) => sales.y,
        markerSettings: MarkerSettings(isVisible: true),
        name: widget.dataList[i]['tabName']),).toList();
    return <SplineSeries<ChartSampleData, String>>[
      SplineSeries<ChartSampleData, String>(
        name: '出入库信息数量',
        dataSource: chartData[0],
        xValueMapper: (ChartSampleData sales, _) => sales.x,
        yValueMapper: (ChartSampleData sales, _) => sales.y,
        markerSettings: MarkerSettings(isVisible: true),
      ),
      // SplineSeries<DataImageModel, String>(
      //   name: '出入库总价',
      //   dataSource: dataImageList,
      //   markerSettings: MarkerSettings(isVisible: true),
      //   xValueMapper: (DataImageModel sales, _) => sales.time,
      //   yValueMapper: (DataImageModel sales, _) => sales.count,
      // ),
      // SplineSeries<DataImageModel, String>(
      //   name: '出入库产品总数',
      //   dataSource: dataImageList,
      //   xValueMapper: (DataImageModel sales, _) => sales.time,
      //   yValueMapper: (DataImageModel sales, _) => sales.count,
      //   markerSettings: MarkerSettings(isVisible: true),
      // ),
    ];
  }
}

///Chart sample data
class ChartSampleData {
  /// Holds the datapoint values like x, y, etc.,
  ChartSampleData(
      this.x,
      this.y,
      );

  /// Holds x value of the datapoint
  final String? x;

  /// Holds y value of the datapoint
  final num? y;
}
